#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QPen> //画笔
#include <QBrush>
#include <QMessageBox>
#include <QRadialGradient>  //渐变
#include <QCloseEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    scene = new QGraphicsScene(this);
    view = new QGraphicsView(this);
    view->setScene(scene);  //将场景关联到视图上
    view->setRenderHint(QPainter::Antialiasing);//设置为抗锯齿
    view->setBackgroundBrush(Qt::NoBrush);  //将视图背景设置为透明，使场景颜色可见
    scene->setBackgroundBrush(QBrush(QColor(210,180,140))); //设置背景色
    setCentralWidget(view); //将视图设置为中心部件
    currentPlayer = true;
    for(int i = 0;i < BOARD_size; ++i)
        for(int j = 0;j < BOARD_size; ++j)
            board[i][j] = 0;

    initBoard();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initBoard()
{
    QPen gridpen(QColor(0,0,0),2);
    //绘制水平网格线
    for(int i = 0;i < BOARD_size; ++i)
        scene->addLine(0,i*CELL_size,(BOARD_size-1)*CELL_size,i*CELL_size,gridpen);
    for(int i = 0;i < BOARD_size;++i)
        scene->addLine(i*CELL_size,0,i*CELL_size,(BOARD_size-1)*CELL_size,gridpen);

}

void MainWindow::DrawPiece(int x, int y)
{
    QRadialGradient gradient(0.5,0.5,0.5);
    //判断是黑棋还是白棋
    if(currentPlayer)
    {
        gradient.setColorAt(0,QColor(128,128,128));
        gradient.setColorAt(1,QColor(0,0,0));
    }
    else
    {
        gradient.setColorAt(0,QColor(255,255,255));
        gradient.setColorAt(1,QColor(180,180,180));
    }
    //设置渐变的坐标模式为相对与对象的边界框
    gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
    QBrush brush(gradient);
    QGraphicsEllipseItem *tuoyuan =
        new QGraphicsEllipseItem(x * CELL_size - CELL_size / 4,y * CELL_size - CELL_size / 4,CELL_size / 2,CELL_size / 2);
    tuoyuan->setBrush(brush);
    tuoyuan->setPen(Qt::NoPen);
    scene->addItem(tuoyuan);
}

bool MainWindow::Win(int x, int y)
{
    int player = board[x][y];
    int count;
    //判断横向
    count = 1;
    //从右往左检查
    for(int i = x - 1;board[i][y] == player && i >= 0; --i)
        ++count;
    //从左往右检查
    for(int i = x+1;board[i][y] == player && i < BOARD_size; ++i)
        ++count;
    if(count >= 5)
        return true;

    //判断纵向
    count = 1;
    //从下向上
    for(int i = y - 1;board[x][i] == player && i >= 0; --i)
        ++count;
    //从上往下
    for(int i = y+1;board[x][i] == player && i<= BOARD_size; ++i)
        ++count;
    if(count >= 5)
        return true;

    //对角线(四种)
    count = 1;
    //正对角
    //往左上走
    for(int i = x - 1,j = y - 1;board[i][j] == player && i >= 0 && j >= 0; --i , --j)
        ++count;
    //往右下走
    for(int i = x + 1,j = y + 1;board[i][j] == player && i <= BOARD_size && j <= BOARD_size; ++i , ++j)
        ++count;
    if(count >= 5)
        return true;

    //反对角
    for(int i = x - 1,j = y + 1;board[i][j] == player && i >= 0 && j <= BOARD_size; --i , ++j)
        ++count;

    for(int i = x + 1,j = y - 1;board[i][j] == player && i <= BOARD_size && i >= 0; ++i , --j)
        ++count;
    if(count >= 5)
        return true;

    return false;
}


void MainWindow::mousePressEvent(QMouseEvent *event)
{
    QPointF pos = view->mapToScene(event->pos());
    int x = static_cast<int>(pos.x()/CELL_size+0.5);
    int y = static_cast<int>(pos.y()/CELL_size+0.5);
    //判断点位上是否有棋子
    if(x>=0&&x<BOARD_size &&y>=0&&y<BOARD_size&&board[x][y] == 0 )
    {
        board[x][y] = currentPlayer?1:2;
        //画棋子
        DrawPiece(x,y);
        if(Win(x,y))
        {
            QString message  = currentPlayer?"黑棋赢了":"白棋赢了";
            QMessageBox::information(this,"Game Over",message);
            close();
        }
        else//切换玩家
            currentPlayer = !currentPlayer;
    }
}

